package com.xcw.bb.serviceImpl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.xcw.bb.dao.AccusationDao;
import com.xcw.bb.dao.InteractDao;
import com.xcw.bb.dao.UserDao;
import com.xcw.bb.daoImpl.AccusationDaoImpl;
import com.xcw.bb.daoImpl.InteractDaoImpl;
import com.xcw.bb.daoImpl.UserDaoImpl;
import com.xcw.bb.entity.Course;
import com.xcw.bb.entity.Interact;
import com.xcw.bb.entity.User;
import com.xcw.bb.service.InteractService;
import com.xcw.bb.util.JdbcUtil;

public class InteractServiceImpl implements InteractService {
    private InteractDao interactDao=new InteractDaoImpl();
    private UserDao udao = new UserDaoImpl();
    private AccusationDao accusationDao=new AccusationDaoImpl();
	@Override
	public List<Interact> queryAllstudentInteract(int id) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			List<Interact> list=interactDao.findAllstudentInteract(id);
			if(list.size()==0) throw new RuntimeException("网站查无数据");
			conn.commit();
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
				
	}
	@Override
	public List<Interact> queryAllLikeInteract(String answer, String select,
			String value, int id) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			List<Interact> list=new ArrayList<Interact>();
			if("teacher_department".equals(select)){
				list=interactDao.findLikeStudentInteractOfTeacherDepartment(answer, value, id);
				if(list.size()==0) throw new RuntimeException("网站查无数据");
				
			}
			if("teacher_name".equals(select)){
				list=interactDao.findLikeStudentInteractOfTeacherName(answer, value, id);
				if(list.size()==0) throw new RuntimeException("网站查无数据");
				
			}
			conn.commit();
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
		
	}
	@Override
	public List<User> queryAllTeacher(){
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			List<User> listUser=interactDao.findAllTeacher();
			
			for(User t:listUser){
				List<Course> listCourse=interactDao.findCourseByTeacherId(t.getUserId());
				t.setCourse(listCourse);
			}
			conn.commit();
			return listUser;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}
	/*
	public void insertInteract( String title,String detail, User student,User teacher) throws Exception{
		Connection conn=null;
		try{
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			Interact interact = new Interact();
			interact.setInteractName(title);
			interact.setInteractDetail(detail);
			interact.setStudentName(student.getRealname());
			interact.setDepartment(student.getDepartment());
			interact.setFlagAnswer("n");
			interact.setCreateDate(new java.util.Date());
			interact.setFlagEstimate("n");
			interact.setTeacherDepartment(teacher.getDepartment());
			interact.setFlagStudentAccusation("n");
			interact.setFlagTeacherAccusation("n");
			interact.setTuId(teacher.getUserId());
			interact.setTeacherName(teacher.getRealname());
			//int id = interactDao.insertInteract(interact);
			conn.commit();
			//return id;
		}catch(Exception e){
			
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}*/
	
	@Override
	public void deleteOneInteract(int id) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			interactDao.deleteOneInteract(id);
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}
	@Override
	public Interact queryInteractDetail(int id) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			Interact interact=interactDao.findInteractDetail(id);
			conn.commit();
			return interact;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}
	
	@Override
	public void updateByEvaluate(int id, String content) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			interactDao.updateByEvaluate(id, content);
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
		
	}
	@Override
	public void updateByUpdate(int id, String content) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			interactDao.updateOneInteractContent(id, content);
			
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
		
	}
	@Override
	public void updateByAccusation(int id, String content) throws Exception {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			interactDao.updateOneInteractAccusationFlag(id);
			accusationDao.insertOneAccusation(id, content);
			
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			if(conn!=null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}
	@Override
	public void insertInteract(String interactName, String interactDetail,
			String department, String studentName, String teacherName,
			Integer tuid, Integer sid, Date createDate, String teacherDepartment) {
		// TODO Auto-generated method stub
		Connection conn=null;
		try{
			conn=JdbcUtil.getConnection();
			conn.setAutoCommit(false);
			
			//判断条件
			if(interactName.length()>50) throw new RuntimeException("互动标题过长！");
			if(interactDetail.length()>2000) throw new RuntimeException("互动内容过长！");
			
			Interact interact=new Interact(null,interactName,interactDetail,  department,  studentName, teacherName,null,  null, null,  null,  tuid, sid, null,  createDate,  null, null,  teacherDepartment, null,  null);
			//Integer id=interactDao.insertInteract(interact);
			//if(id==null) throw new RuntimeException("提交失败！");
			conn.commit();
			//return id;
		}catch(Exception e){
			if(conn!=null)try{conn.rollback();}catch(Exception ee){}
			throw new RuntimeException(e.getMessage());
		}finally{
			JdbcUtil.close(null, null, conn);
		}
	}

}
